<?php

namespace app\customer\logic;
use app\admin\logic\TestLogic;
use app\common\library\Upload;
use app\common\service\UploadService;
use app\data\util\MqQueueName;
use app\systems\logic\MqTaskPushLogic;
use think\facade\Config;
use think\facade\Db;
use app\data\util\HttpUtil;
use app\common\library\RedissLock;

class BatchMessageLogic extends BasicLogic
{

    /**
     * @desc   批量发送消息列表
     * @author luckyXiong
     * @date   2024-05-24
     */
    public function get_private_messages($params=[]){

        $shop_id            = $params['shop_id']??[];
        $send_status        = $params['send_status']??'';
        $created_time       = $params['created_time']??'';
        $limit              = $params['limit']??'10';
        $where              = [];

        if($shop_id){
            $where[] = ['shop_id','in',$shop_id];
        }

        if($send_status){
            $where[] = ['send_status','=',$send_status];
        }

        if ($created_time) {
            $created_time = explode(',', $created_time);
            $where[] = ['createtime', '>=', strtotime($created_time[0])];
            $where[] = ['createtime', '<=', strtotime($created_time[1])];
        }

        $res = Db::name('im_batch_conversations')
            ->order('createtime desc')
            ->where($where)
            ->paginate($limit);

        $list =   $res->items()?:[];

        //批量获取店铺信息及sku数量信息;
        $shop_ids      = array_column($list,'shop_id');
        $shopInfos     = Db::name('shops')->whereIn('id',$shop_ids)->field('name,id')->select();
        $shopInfos     = objectToArray($shopInfos);
        $shopInfos_map = array_column($shopInfos,null,'id');
        foreach($list as $k=>$v){
            $shop_name                        = $shopInfos_map[$v['shop_id']]??[];
            $list[$k]['shop_name']            = $shop_name['name']??'';
            $list[$k]['created_time']         = date("Y-m-d H:i:s", $v['createtime']);
        }
        return [
            'list'   => $list,
            'total'  => $res->total(),
        ];
    }







    /**
     * @desc   批量发送消息消费方法
     * @author luckyXiong
     * @date   2024-05-24
     */
    public function batch_send_messages($params=[]){
        $ids  = $params['ids']??'';
        $text = $params['content']??'';
        if(!$ids)   return  '请选择发送数据!';
        $list =  Db::name('im_batch_conversations')->whereIn('id',$ids)
            ->field('id,shop_id,order_id,buyer_user_id')
            ->select();

        $list = objectToArray($list);
        if($list){
            foreach($list as $v){
                //数据推送到队列处理
                MqTaskPushLogic::mq_task_push(
                    '',
                    MqQueueName::QUEUE_PRIVATE_SEND_MESSAGE_QUEUE,
                    [
                        'shop_id'        =>$v['shop_id'],
                        'order_id'       =>$v['order_id'],
                        'type'           =>3,
                        'text'           =>str_replace(array("\r", "\n"), '', $text),
                    ],
                    false
                );
            }
        }



    }





    /**
     * @desc   批量发送消息消费方法
     * @author luckyXiong
     * @date   2024-05-24
     */
    public function private_send_message_consumer($info){
        //并发处理;
        $redis_lock = new RedissLock();
        $key        = 'operate_order_consumer:'.time();
        $lock       = $redis_lock::lock($key, $redis_lock::UNBLOCK, 500);
        if (!$lock) {
            return ['code'=>201,'msg'=>'数据并发了'];
        }
        htmlLog(['data'=>$info],OPERATE_LOG . date('Ym') . '/private_send_message_consumer/' . date('Ymd') . '.txt');
        //调用接口
        (new TikMessageLogic())->create_conversations($info);
        $lock->unlock();
        return true;
    }









}